<!-- src/views/question/questions.vue -->
<template>
  <div class="exam">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="题库名称">
        <el-input v-model="subject" placeholder="题库名称"></el-input>
      </el-form-item>
      <el-form-item label="题目内容">
        <el-input v-model="question" placeholder="题目内容"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getAnswerInfo" icon="el-icon-a-042" style="font-size: 20px;">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="pagination.records" border :row-class-name="tableRowClassName">
      <el-table-column fixed="left" prop="subject" label="题库名称" width="180"></el-table-column>
      <el-table-column prop="question" label="题目信息" width="490"></el-table-column>
      <el-table-column prop="type" label="题目类型" width="200"></el-table-column>
      <el-table-column prop="score" label="试题分数" width="150"></el-table-column>
      <el-table-column prop="rightAnswer" label="答案" width="120"></el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.current"
      :page-sizes="[6, 10]"
      :page-size="pagination.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      class="page"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pagination: {
        current: 1,
        total: null,
        size: 6
      },
      subject: "",
      question: ""
    };
  },
  created() {
    // ✅ 获取从题库列表传来的 subject
    const subject = this.$route.query.subject;
    if (subject) {
      this.subject = subject; // 自动填入输入框
    }
    // ✅ 自动触发查询（带分页）
    this.getAnswerInfo();
  },
  methods: {
    toEdit(type, id) { 
      this.$router.push({
        path: '/editAnswerChildren',
        query: { type, questionId: id }
      });
    },
    getAnswerInfo(size, current) {
      // console.log('我已查询 :>> ');
      // 更新分页参数
      if (typeof size === 'number' && !isNaN(size)) {
        this.pagination.size = size;
      }
      if (typeof current === 'number' && !isNaN(current)) {
        this.pagination.current = current;
      } else {
        this.pagination.current = 1;
      }

      // ✅ 处理空字符串：用 "@" 防止模糊匹配所有
      const searchSubject = this.subject.trim() || "@";
      const searchQuestion = this.question.trim() || "@";

      this.$axios(`/api/answers/${this.pagination.current}/${this.pagination.size}/${searchSubject}/${searchQuestion}`)
        .then(res => {
          this.pagination = res.data.data;
        })
        .catch(error => {
          console.error('查询题目失败:', error);
          this.$message.error('加载题目失败，请检查网络或接口');
        });
    },
    handleSizeChange(val) {
      this.getAnswerInfo(val);
    },
    handleCurrentChange(val) {
      this.getAnswerInfo(undefined, val);
    },
    tableRowClassName({ row, rowIndex }) {
      return rowIndex % 2 === 0 ? "warning-row" : "success-row";
    }
  }
};
</script>

<style lang="less" scoped>
.exam {
  padding: 0px 40px;
  .page {
    margin-top: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .edit {
    margin-left: 20px;
  }
  .el-table .warning-row {
    background: #000 !important;
  }
  .el-table .success-row {
    background: #DD5862;
  }
}
</style>